import Vue from 'vue'
import Router from 'vue-router'
import routeList from './routeList'
import baseInfo from './utils/config'
Vue.use(Router)

let router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: routeList
})
let treeToArray = (array, tree) => {
  tree.forEach(item => {
    if (item.path !== '/404' && item.path !== '/' && item.path !== '/login') {
      array.push(item.path)
    }
    if (item.childNodes && item.childNodes.length > 0) {
      treeToArray(array, item.childNodes)
    }
  })
  return array
}
/* const whiteList = ['/login', '/authredirect', '/register']
router.beforeEach((to, from, next) => {
  document.title = baseInfo.BASE_TITLE + '-' + to.meta.text
  if (localStorage.tokenInfo && localStorage.menuTree && localStorage.userInfo) {
    let rightList = treeToArray([], JSON.parse(localStorage.menuTree))
    if (to.path === '/login') {
      next({ path: '/' })
    }
    if (to.path === '/404' || to.path === '/' || to.path === '/login') {
      next()
    } else {
      // eslint-disable-next-line no-unused-vars
      let hasRouter = 0
      rightList.forEach(item => {
        if (to.path.indexOf(item) !== -1) {
          hasRouter++
        }
      })
      next()
      // if (hasRouter > 0) {
      //   next()
      // } else {
      //   next('/404')
      // }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
}) */
export default router
